<template>
  <div>
    <!-- 搜索 -->
    <!-- 技术报告录入 -->
    <TopFilter ref="TopFilters" :list="topFilterInit" :cols="3" :collapse="collapse" labelWidth="110" @filterChange="changeHandle"></TopFilter>
    <!-- table -->
    <FilterTable ref="table" :columns="columns" :params="fetchParams" :dataSource="dataList" :datakey="'records'" :fetchapi="fetchApi" :onColumnsChange="columns => (this.columns = columns)" :onRowSelectChange="selectChange" :is-select-column="false">
      <template slot="controls" slot-scope="props">
        <multiuse-button size="small" type="primary" icon="el-icon-plus" @click="add()">新增</multiuse-button>
                <multiuse-button size="small" type="primary" icon="el-icon-download" :click="downloadTemplate.bind()" v-if="appId == 'beijing'">下载模板</multiuse-button>
        <multiuse-button size="small" icon="el-icon-upload" @click="importFile()" v-if="appId == 'beijing'">导入</multiuse-button>
      </template>
    </FilterTable>
    <Drawer :visible.sync="visibleAdd" :title="'技术报告录入新增'" destroyOnClose width="100%" :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}">
      <AddForm @close="closeAdd" @save="saveAdd"></AddForm>
    </Drawer>
    <Drawer :visible.sync="visibleEdit" :title="'技术报告录入编辑'" destroyOnClose width="100%" :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}">
      <EditForm @close="closeEdit" @save="saveEdit" :rowData="rowData" ></EditForm>
    </Drawer>
    <BaseDialog :visible.sync="visible" :title="'车型选择'" destroyOnClose width="850px" :containerStyle="{height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px'}">
      <ModelVehicle @close="close" @save="save"></ModelVehicle>
    </BaseDialog>
    <BaseDialog :visible.sync="showImport" :closable="false" icon="el-icon-upload" title="导入" width="800px" destroyOnClose :containerStyle="{ height: 'calc(100% - 120px)', overflow: 'auto', paddingBottom: '60px' }">
      <importExcel @close="showImport = false" @importclose="editsave()" url="/api/cms_claimdealer/rPTech/importExcel"/>
    </BaseDialog>
  </div>
</template>
<script>
import AddForm from './AddForm.vue';
import EditForm from './EditForm.vue';
import ModelVehicle from '@/pages/claimDemage/tools/ModelVehicle.vue';
//后台请求
import { getListByPage ,updateStatusById, getImportExcel }  from '@/api/technicalReportManagement/technicalSupportManagement_DLR/technicalReportEntry/index';
//字典
import { getPartType } from '@/api/dictionary/index';
//获取当前经销商
import {getOwnerCode} from '@/assets/js/auth';
import importExcel from '@/pages/claimDemage/tools/importExcel.vue';
import { exportExcelFile } from '@/utils/index';
import { getAppId } from '@/assets/js/auth';

export default {
  components: {
    AddForm,
    EditForm,
    ModelVehicle,
    importExcel
  },
  data() {
    return {
      dataList: [],
      visible: false,
      collapse: true,
      topFilterInit: this.topFilterInitFun(),
      columns: this.creatHeader(),
      fetchApi: getListByPage,
      fetchParams: {
        //单据状态草稿
        techStatus: 55521002,
        ownerCode: getOwnerCode(),
        xhrAbort: false 
      },
      selectedRows: [], //选中
      visibleAdd: false,
      rowData: null,
      visibleEdit: false,
      showImport: false,
      appId: getAppId()
    };
  },
  created() {
    this.chuqi();
  },
  methods: {
    downloadTemplate() {
      this.$confirm('此操作将下载excel模板, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          getImportExcel().then(res => {
            exportExcelFile(res.data, '技术报告导入模板');
          });
        });
    },
    importFile() {
      this.showImport = true;
    },
    editsave() {
      this.showImport = false;
      this.fetchParams = { ...this.fetchParams };
    },
    async chuqi() {
      //是否首次咨询
      const res1 = await getPartType({type: 5500});
      if (res1.resultCode == 200) {
        this.topFilterInit.find(x => x.fieldName == 'ifFirst').initialValue = '';
        this.topFilterInit.find(x => x.fieldName == 'ifFirst').itemList = res1.data.map(y => ({text: y.codeCnDesc, value: y.codeId}));
      }
    },
    //选中
    selectChange(rows) {
      this.selectedRows = rows;
      console.log(this.selectedRows);
    },
    chooseOrderNo() {
      this.visible = true;
    },
    exportFileBtn() {
      this.$refs.TopFilters.SUBMIT_FORM();
    },
    add() {
      this.visibleAdd = true;
    },
    edit(val) {
      this.rowData = val;
      console.log("编辑ID",val)
      this.visibleEdit = true;
    },
    async delete(val) {
      console.log("作废ID", val)
      this.$confirm('此操作将删除该条数据, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
         updateStatusById({techId : val.techId}).then(res=>{
          if(res.resultCode === 200){
            this.$message.success('作废成功!');
            this.fetchParams = { ...this.fetchParams };
          }else{
            this.$message({
              type: 'error',
              message: res.data.errMsg
            });
          }
        })
      })
    },
    closeAdd() {
      this.visibleAdd = false;
    },
    saveAdd() {
      this.visibleAdd = false;
      this.fetchParams = { ...this.fetchParams };
    },
    closeEdit() {
      this.visibleEdit = false;
    },
    saveEdit() {
      this.visibleEdit = false;
      this.fetchParams = { ...this.fetchParams };
    },
    close() {
      this.visible = false;
      this.fetchParams = { ...this.fetchParams };
    },
    save(obj) {
      this.visible = false;
      this.topFilterInit.find(x => x.fieldName == 'modelName').initialValue = obj[0].modelName;
    },
    //搜索获取数据
    changeHandle(val) {
      //单据状态草稿
      this.fetchParams = Object.assign({},val,{ techStatus: 55521002, ownerCode: getOwnerCode()},{xhrAbort: false});
      console.log(val);
    },
    //搜索数据
    topFilterInitFun() {
      return [
        { type: 'INPUT', label: 'VIN', fieldName: 'vin', placeholder: '请输入VIN...', initialValue: '' },
        { type: 'INPUT', label: '技术报告单号', fieldName: 'techNo', placeholder: '请输入技术报告单号...', initialValue: '' },
        { type: 'SELECT', label: '是否首次咨询', fieldName: 'ifFirst', placeholder: '请选择是否首次咨询...', initialValue: '', itemList: [] },
        // { type: 'RANGE_DATE', label: '提交日期', fieldName: 'beginApplyDate|endApplyDate', placeholder: '请输入', initialValue: '' },
        {
          type: 'INPUT',
          label: '车型',
          fieldName: 'modelName',
          placeholder: '请输入车型...',
          initialValue: '',
          unitRender: p => {
            return (
              <div>
                <multiuse-button icon="el-icon-search" onClick={() => this.chooseOrderNo()} />
              </div>
            );
          }
        }
      ];
    },
    //table表头和数据源
    creatHeader() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          fixed: 'left',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 100,
          render: props => {
            return (
              <div style="text-align:center">
                <multiuse-button size="mini" type="text" onClick={() => this.edit(props.row)}>
                  编辑
                </multiuse-button>
                <el-divider direction="vertical"/>
                <multiuse-button size="mini" type="text" onClick={() => this.delete(props.row)}>
                  作废
                </multiuse-button>
              </div>
            );
          }
        },
        {title: '状态', dataIndex: 'statusStr', sorter: true},
        {title: '技术报告单号', dataIndex: 'techNo', sorter: true},
        {title: '是否首次咨询', dataIndex: 'ifFirstStr', sorter: true},
        {title: '经销商联系人', dataIndex: 'dcPerson', sorter: true},
        {title: '经销商联系人电话', dataIndex: 'dcTel', sorter: true},
        //与BA沟通 录入界面没有提交时间
        // { title: '提交时间', dataIndex: 'firstDate', sorter: true ,dateFormat: 'yyyy-MM-dd'},
        {title: 'VIN', dataIndex: 'vin', sorter: true, width: 150},
        {title: '车型', dataIndex: 'modelName', sorter: true},
        {title: '行驶里程', dataIndex: 'driveMile', sorter: true},
        {title: '生产日期', dataIndex: 'prodDate', sorter: true, dateFormat: 'yyyy-MM-dd'},
        {title: '销售日期', dataIndex: 'saleDate', sorter: true, dateFormat: 'yyyy-MM-dd'},
        {title: '车牌号', dataIndex: 'licenseNo', sorter: true},
        {title: '受理类型', dataIndex: 'acceptTypeStr', sorter: true}
      ];
    }
  }
};
</script>

<style lang="less" scoped></style>
